﻿//-----------------------------------------------------------------------
// <copyright file="Program.cs" company="DylanBox">
// Copyright (c) DylanBox Corporation. All rights reserved.
// </copyright>
// <summary>
//
//  This demo come from test experience,
//  we need some clients to send requests to service, and read values of service perfCounter
//
//  I can learn:
//      1. Action is a generic delegate;
//      2. calculate average value, except zero
//           aList.Where<float>(delegate(float r)
//             {
//                return (r != 0f);
//             }).Average();
//      3. get action list's not finished number
//            List<IAsyncResult> clientList = new List<IAsyncResult>();
//            action = new Action<int>(SendRequestFunc);
//            for(...) {clientList.Add(action.BeginInvoke(duritionTime, null, null)); }
//            clientList.Where<IAsyncResult>(delegate(IAsyncResult r)
//            {
//                return r.IsCompleted;
//            }).Count<IAsyncResult>();
// </summary>
// <reference>
//    
// </reference>
// <author>Dylan</author>
//-----------------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Diagnostics;

namespace AsyncClients
{
    class Program
    {

        static void call(IAsyncResult res)
        {
        }

        static void Main(string[] args)
        {
            
            Action thr = new Action(
                () =>
                {
                    Thread.Sleep(1000 * 5);
                    Console.WriteLine("sub {0} thread", Thread.CurrentThread.ManagedThreadId);
                }
            );

            IAsyncResult res = thr.BeginInvoke(new AsyncCallback(call), null);

            res.AsyncWaitHandle.WaitOne();
                
            thr.EndInvoke(res);

            Console.WriteLine("main {0} thread", Thread.CurrentThread.ManagedThreadId);

            

            //Action<int> action = null;
            //List<IAsyncResult> clientList = new List<IAsyncResult>();

            //int duritionTime = 40;
            //int clientNum = 6;
            //int currentFinishedClient = 0;

            //action = new Action<int>(SendRequestFunc);

            //// make the clients, every client will send requests to service for "duritionTime" seconds
            //for (int i = 0; i < clientNum; i++)
            //{
            //    clientList.Add(action.BeginInvoke(duritionTime, null, null));
            //}

            //List<float> DBRequestsPerSecResultList = new List<float>();
           
            //PerformanceCounter DBRequestsPerSecCounter = new PerformanceCounter()
            //{
            //    MachineName = ".",
            //    CategoryName = "Services",
            //    CounterName = "Service.V1.DBRequestsPerSec"
            //};

            //do
            //{
            //    Thread.Sleep(0x3e8); // wait one second
                
            //    // Read the nextValue of perfCounter, save it into a List
            //    DBRequestsPerSecResultList.Add(DBRequestsPerSecCounter.NextValue());

            //    currentFinishedClient = clientList.Where<IAsyncResult>(delegate(IAsyncResult r)
            //    {
            //        return r.IsCompleted;
            //    }).Count<IAsyncResult>();
            //}
            //while (currentFinishedClient < clientNum);

            //float actualValue = DBRequestsPerSecResultList.Where<float>(delegate(float r)
            //{
            //    return (r != 0f);
            //}).Average();
        }

        /// <summary>
        /// Every client use this function to send requests to service for <timsSpan> seconds
        /// </summary>
        /// <param name="timeSpan"></param>
        private static void SendRequestFunc(int timeSpan)
        {
            Stopwatch stopwatch = new Stopwatch();
            double totalSeconds = 0.0;
            do
            {
                stopwatch.Start();
                //WebServiceHelper.SendRequestToDeliveryServiceLookupAPI(WebServiceURL);
                stopwatch.Stop();
                totalSeconds = stopwatch.Elapsed.TotalSeconds;
            }
            while (totalSeconds < timeSpan);
        }
    }
}
